package com.acmedcare.framework.exchange.center.endpoint.util;

import com.acmedcare.framework.exchange.center.entity.dto.Hdfs;
import java.io.File;
import java.net.URI;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

@Slf4j
public class HDFSCommon {

  static public String copyfileToHdfs(Configuration config, Hdfs hdfs, File file) {
    try {
      config.set("fs.defaultFS", hdfs.getUri());
      FileSystem fs = FileSystem.get(URI.create(hdfs.getUri()), config, hdfs.getUser());
      Path src = new Path(file.getPath());
      Path dst = new Path(
          hdfs.getUri() + File.separator + "acmedcare" + File.separator + file.getName());
      fs.copyFromLocalFile(src, dst);
      return dst.toString();
    } catch (Exception e) {
      log.error("HDFS 文件上传失败!", e);
    }
    return "";
  }
}